// Copyright (c) 2024, json_wang and contributors
// For license information, please see license.txt

frappe.ui.form.on("CP_Opportunity", {
	refresh(frm) {
		frm.add_custom_button('转合同', () => {
			console.log(frm);
			if (frm.doc.opportunity_from != "Customer") {
				//frappe.throw("This is an Error Message")
				frappe.show_alert("请将商机的潜在客户修改为正式客户再转合同!", 5);
			}
			frm.call('make_contract', { throw_if_missing: true })
				.then(r => {
					if (r.message) {
						frappe.set_route("Form", "CP_Contract", r.message.name);
					}
				})

		})
	},
	setup: function (frm) {
		frm.set_query("opportunity_from", function () {
			return {
				filters: {
					name: ["in", ["Customer", "CP_Prospect"]],
				},
			};
		});
	},
});
